<?php
namespace app\passenger\model;

use app\BaseModel;
use think\facade\Db;

class IndexActive extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_passenger_active';


    protected $append = [

        'status_text'
    ];

    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }



    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-03-21 15:00
     */
    public function getStatusTextAttr($value,$data){

        if(!empty($data['start_time'])&&$data['end_time']){
            if($data['start_time']>time()){

                return '未开始';

            }elseif ($data['start_time']<time()&&$data['end_time']>time()){

                return '进行中';

            }else{

                return '已结束';
            }
        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:转换图片格式
     * @author chenniang
     * @DataTime: 2020-03-09 18:56
     */
    public function getImgsAttr($value){

        $data = transImages([$value],['imgs']);

        return !empty($data[0])?$data[0]:[];

    }


    /**
     * @param $value
     * @功能说明:封面图
     * @author chenniang
     * @DataTime: 2020-03-09 19:02
     */
    public function getCoverAttr($value,$data){

        $img = transImagesOne([$value],['cover'],$data['uniacid']);

        return !empty($img[0])?$img[0]:'';
    }

    /**
     * @param $dis
     * @param $page
     * @功能说明:活动列表
     * @author chenniang
     * @DataTime: 2020-03-09 18:42
     */

    public function activeList($dis,$page){

        $data = $this->where($dis)->order(['id desc'])->paginate($page)->toArray();

        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:活动详情
     */
    public function activeInfo($to_uid,$uniacid,$id,$field='a.*',$status=0){

        $company_id = Db::name('longbing_card_user_info')->where(['fans_id'=>$to_uid])->value('company_id');

        $dis[] = ['a.uniacid','=',$uniacid];

        if(empty($status)){
            //状态
            $dis[] = ['a.status','=',1];
        }
        if(empty($id)){
            //开始时间
            $dis[] = ['a.start_time','<',time()];
            //结束时间
            $dis[] = ['a.end_time','>',time()];
            //公司
            $dis[] = ['b.company_id','=',$company_id];

        }else{
            //状态
            $dis[] = ['a.id','=',$id];
        }

        $data = $this->alias('a')
                ->join('longbing_card_passenger_company b','b.active_id = a.id')
                ->where($dis)
                ->field($field)
                ->find();

        return !empty($data)?$data->toArray():[];
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 16:03
     * @功能说明:活动详情
     */
    public function activeDetail($dis){

        $data = $this->where($dis)->status(-1)->find();

        return !empty($data)?$data->toArray():[];
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:员工端活动列表
     */
    public function staffActiveList($to_uid,$uniacid,$status=1){

        $company_id = Db::name('longbing_card_user_info')->where(['fans_id'=>$to_uid])->value('company_id');

        $dis[] = ['a.uniacid','=',$uniacid];
        //状态
        $dis[] = ['a.status','=',1];
        //进行中
        if($status==1){
            //开始时间
            $dis[] = ['a.start_time','<',time()];
            //结束时间
            $dis[] = ['a.end_time','>',time()];
        }elseif ($status==2){
            //未开始
            $dis[] = ['a.start_time','>',time()];
        }else{
            //已经结束
            $dis[] = ['a.end_time','<',time()];
        }
        //公司
        $dis[] = ['b.company_id','=',$company_id];

        $data = $this->alias('a')
            ->join('longbing_card_passenger_company b','b.active_id = a.id')
            ->where($dis)
            ->field('a.*')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate(10)
            ->toArray();

        if(!empty($data['data'])){

            $coupon_model = new Coupon();

            foreach ($data['data'] as &$v){

                $dis = [
                    //员工
                    'staff_id' => $to_uid,
                    //活动
                    'active_id'=> $v['id'],
                    //类型
                    'type'     => 1
                ];
                //总领取
                $v['all_coupon_num'] = $coupon_model->where($dis)->count();
                //总使用
                $dis['status'] = 2;

                $v['use_coupon_num'] = $coupon_model->where($dis)->count();

                $v['start_time'] = date('Y-m-d H:i:s',$v['start_time']);

                $v['end_time']    = date('Y-m-d H:i:s',$v['end_time']);
            }
        }
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:13
     * @return void
     * descrption:编辑活动
     */
    public function activeUpdate($dis,$data){
        //更新时间
        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 19:03
     * @功能说明:获取活动状态
     */

    public function getActiveStatus(){


    }














}